using System.Collections.Generic;
using System.Linq;

namespace SynoManager.FileStation
{
    public class DirectoryInfo : IRemoteLocation
    {
        protected readonly FileStationService service;

        protected string name;
        private string fullpath;

        internal DirectoryInfo(FileStationService service, string fullpath)
        {
            this.service = service;
            this.fullpath = fullpath;
        }

        public DirectoryInfo(FileStationService service, string fullpath, FileStationClient.GetFilesResult files)
            : this(service, fullpath)
        {
            filesResult = files;
        }

        public DirectoryInfo(FileStationService service, FileStationClient.GetFileResultItem directoryData)
            : this(service, directoryData.path)
        {
            name = directoryData.filename;
        }

        public string Name
        {
            get { return name; }
            set
            {
                service.client.Rename(ParentPath, name, value);
                this.name = value;
            }
        }

        public string RelativePath
        {
            get { return fullpath.Substring(fullpath.IndexOf('/', 1)); }
        }

        /// <summary>
        /// Gets the (relative) path of the parent folder
        /// </summary>
        public string ParentPath
        {
            get
            {
                string relpath = RelativePath;
                string result = relpath.Substring(0, relpath.LastIndexOf('/'));
                return result;
            }
        }

        public string Volume
        {
            get { return fullpath.Split('/')[1]; }
        }

        public string Path
        {
            get { return fullpath; }
        }

        private FileStationClient.GetFilesResult filesResult;
        private DirectoryInfo[] directories;
        private FileInfo[] files;

        protected void LoadContents()
        {
            filesResult = service.client.GetFiles(RelativePath, "all", 0, 999, "filename", "ASC");
            List<DirectoryInfo> dirList = new List<DirectoryInfo>();
            foreach (var directory in filesResult.items.Where(x => x.isdir))
            {
                dirList.Add(new DirectoryInfo(service, directory));
            }
            directories = dirList.ToArray();
            List<FileInfo> fileList = new List<FileInfo>();
            foreach (var file in filesResult.items.Where(x => !x.isdir))
            {
                fileList.Add(new FileInfo(service, file));
            }
            files = fileList.ToArray();
        }

        public virtual DirectoryInfo[] GetDirectories()
        {
            if (filesResult == null)
                LoadContents();

            return directories;
        }

        public virtual FileInfo[] GetFiles()
        {
            if (filesResult == null)
                LoadContents();

            return files;
        }

        public virtual void CreateFolder(string name)
        {
            service.client.CreateFolder(RelativePath, name);
        }

        internal void Reload()
        {
            filesResult = null;
        }
    }
}